package com.sdp.irrigation.controller;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.irrigation.cache.IrrigationCleanCache;
import com.sdp.irrigation.db.irrigation_emp_pump_well.service.IIrrigationEmpPumpWellService;
import com.sdp.irrigation.db.irrigation_emp_pump_well.service.impl.IrrigationEmpPumpWellServiceImpl;
import com.sdp.irrigation.entity.IrrigationProject;
import com.sdp.irrigation.view.IrrigationEmpPumpWellView;

/**
 * <p>
 * 人员机井权限信息 前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-12-22
 */
@RestController
@RequestMapping("/IrrigationEmpPumpWellController")
public class IrrigationEmpPumpWellController {
	
	@Autowired
	private IIrrigationEmpPumpWellService iIrrigationEmpPumpWellService;
	
	@Autowired
	private IrrigationEmpPumpWellServiceImpl irrigationEmpPumpWellServiceImpl;
	
	@Autowired
	private IrrigationCleanCache irrigationCleanCache;
	
	@Autowired
	private Query query;
	
	@PostMapping(value = "/viewPage")
	public Result<?> viewPage(@RequestBody DataCenter dataCenter) {
		QueryWrapper<IrrigationEmpPumpWellView> queryWrapper = new QueryWrapper<>();
		return Result.ok(query.viewPage(irrigationEmpPumpWellServiceImpl, IrrigationEmpPumpWellView.class, dataCenter, queryWrapper));
	
	}
	
	
	@PostMapping(value = "/queryPumpWellTree")
	public Result<?> queryPumpWellTree(@RequestBody IrrigationProject irrigationProject){
		if(irrigationProject==null || StringUtils.isEmpty(irrigationProject.getProjectNo())) {
			return Result.error("请先选择项目");
		}
		return Result.ok(iIrrigationEmpPumpWellService.queryPumpWellTree(irrigationProject.getProjectNo()));
	}
	
	@GetMapping(value = "/queryEmpPumpTreeKeys")
	public Result<?> queryEmpFieldAndPumpTreeKeys(@RequestParam(name="projectNo",required=true) String projectNo,@RequestParam(name="empNo",required=true) String empNo) {
		if(StringUtils.isEmpty(projectNo) || StringUtils.isEmpty(empNo)) {
			return Result.error("请先选择人员和项目");
		}
		List<String> nodeKeys = new ArrayList<>();
		nodeKeys.addAll(iIrrigationEmpPumpWellService.queryEmpPumpWellNodeKeys(projectNo, empNo));
		return Result.ok(nodeKeys);
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody JSONObject checked) {
		if(checked==null) {
			return Result.error("人员机井设置信息不存在");
		}
		String projectNo = checked.getString("projectNo");
		if(StringUtils.isEmpty(projectNo)) {
			return Result.error("请先选择某一项目");
		}
		String empNo = checked.getString("empNo");
		iIrrigationEmpPumpWellService.setEmpPumpWell(empNo, checked);
		//删除缓存
		irrigationCleanCache.cleanEmpPumpTree(empNo);
		return Result.ok();
	}

}

